package org.oneblog.data.bean;

import java.util.List;

import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrInputDocument;
import org.oneblog.data.fields.ArticleField;
import org.oneblog.data.util.PojoBeanCreator;
import org.oneblog.data.util.StringTools;
import org.springframework.util.CollectionUtils;

//文章实体类
public class ArticleSolr extends AbstractSolr {

    /**
     */
    private static final long serialVersionUID = 8587279741691603668L;

    private String articleId;
    
    private String articleTitle;
    
    private String articleContent;
    
    private Long updateUserId;
    
    private Long createUserId;
    
    private Long updateTime;
    
    private Long createTime;
    
    private List<String> tags;
    
    //分类
    private Integer categoryId;
    
    //优先级，从高到底，用于首页推荐之类
    private Integer priority;
    
    //来源：转载，原创
    private Integer srcType;

    private String thumbnailImgUrl;//列表缩略图
    
    //统计属性
    //浏览次数
    private Integer browserCount;
    
    private Integer goodCount;
    
    public Integer getGoodCount() {
        return goodCount;
    }

    public void setGoodCount(Integer goodCount) {
        this.goodCount = goodCount;
    }

    @Override
    public SolrInputDocument toSolrInputDocument() {
        SolrInputDocument d = new SolrInputDocument();
        
        d.setField(ArticleField.articleId, this.articleId);
        d.setField(ArticleField.articleTitle, this.articleTitle);
        d.setField(ArticleField.articleContent, this.articleContent);
        d.setField(ArticleField.updateUserId, this.updateUserId);
        d.setField(ArticleField.createUserId, this.createUserId);
        d.setField(ArticleField.updateTime, this.updateTime);
        d.setField(ArticleField.createTime, this.createTime);
        
        if(!CollectionUtils.isEmpty(tags)) {
            for(String tag : tags) {
                d.addField(ArticleField.tags, tag);
            }
            d.setField(ArticleField.TAG_TEXT, StringTools.join(tags, " "));
        }

        d.setField(ArticleField.categoryId, this.categoryId);
        d.setField(ArticleField.priority, this.priority);
        d.setField(ArticleField.srcType, this.srcType);
        d.setField(ArticleField.browserCount, this.browserCount);
        d.setField(ArticleField.THUMBNAIL_IMG_URL, this.thumbnailImgUrl);
        d.setField(ArticleField.goodCount, this.goodCount);
        return d;
    }
    
    public static ArticleSolr createFrom(SolrDocument d) {
        ArticleSolr a = new ArticleSolr();

        a.setArticleId(PojoBeanCreator.getDocValue(d.get(ArticleField.articleId)));
        a.setArticleTitle(PojoBeanCreator.getDocValue(d.get(ArticleField.articleTitle)));
        a.setArticleContent(PojoBeanCreator.getDocValue(d.get(ArticleField.articleContent)));
        a.setUpdateUserId(PojoBeanCreator.getLongValue(d, ArticleField.updateUserId, 0L));
        a.setCreateUserId(PojoBeanCreator.getLongValue(d, ArticleField.createUserId, 0L));
        a.setUpdateTime(PojoBeanCreator.getLongValue(d, ArticleField.updateTime, 0L));
        a.setCreateTime(PojoBeanCreator.getLongValue(d, ArticleField.createTime, 0L));
        a.setTags(PojoBeanCreator.getStringValueList(d, ArticleField.tags));
        a.setCategoryId(PojoBeanCreator.getIntValue(d, ArticleField.categoryId, 0));
        a.setPriority(PojoBeanCreator.getIntValue(d, ArticleField.priority, 0));
        a.setSrcType(PojoBeanCreator.getIntValue(d, ArticleField.srcType, 0));
        a.setBrowserCount(PojoBeanCreator.getIntValue(d, ArticleField.browserCount, 0));
        a.setThumbnailImgUrl(PojoBeanCreator.getDocValue(d.get(ArticleField.THUMBNAIL_IMG_URL)));
        a.setGoodCount(PojoBeanCreator.getIntValue(d, ArticleField.goodCount, 0));
        return a;
    }
    
    public ArticleSolr() {
        
    }

    public String getArticleId() {
        return articleId;
    }

    public void setArticleId(String articleId) {
        this.articleId = articleId;
    }

    public String getArticleTitle() {
        return articleTitle;
    }

    public void setArticleTitle(String articleTitle) {
        this.articleTitle = articleTitle;
    }

    public String getArticleContent() {
        return articleContent;
    }

    public void setArticleContent(String articleContent) {
        this.articleContent = articleContent;
    }

    public Long getUpdateUserId() {
        return updateUserId;
    }

    public void setUpdateUserId(Long updateUserId) {
        this.updateUserId = updateUserId;
    }

    public Long getCreateUserId() {
        return createUserId;
    }

    public void setCreateUserId(Long createUserId) {
        this.createUserId = createUserId;
    }

    public Long getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(Long updateTime) {
        this.updateTime = updateTime;
    }

    public Long getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Long createTime) {
        this.createTime = createTime;
    }

    public List<String> getTags() {
        return tags;
    }

    public void setTags(List<String> tags) {
        this.tags = tags;
    }

    public Integer getCategoryId() {
        return categoryId;
    }

    public void setCategoryId(Integer categoryId) {
        this.categoryId = categoryId;
    }

    public Integer getPriority() {
        return priority;
    }

    public void setPriority(Integer priority) {
        this.priority = priority;
    }

    public Integer getSrcType() {
        return srcType;
    }

    public void setSrcType(Integer srcType) {
        this.srcType = srcType;
    }

    public Integer getBrowserCount() {
        return browserCount;
    }

    public void setBrowserCount(Integer browserCount) {
        this.browserCount = browserCount;
    }

    public String getThumbnailImgUrl() {
        return thumbnailImgUrl;
    }

    public void setThumbnailImgUrl(String thumbnailImgUrl) {
        this.thumbnailImgUrl = thumbnailImgUrl;
    }
}
